import { setItem, getItem } from "@/utils/auth";
import { login } from '@/api/users'
import { ElementPlus } from "@element-plus/icons-vue";

export default {
    namespaced: true,
    state: () => {
        return {
            qwe: getItem('qwe') || 0,
            item: getItem('ysy') || [],
            xiadan1:[]
        }
    },
    mutations: {
        // 传入 state
        sum(state, num) {
            state.qwe++
            state.item.push(num)
            let obj = {}
            state.item = state.item.reduce((element, next) => {
                obj[next.id] ? '' : obj[next.id] = true && element.push(next)
                return element
            }, [])

            state.item.forEach(element => {
                if (element.id === num.id) {
                    element.sum++
                }
            });

            setItem('ysy', state.item)
            setItem('qwe', state.qwe)
        },
        shanchu(state, id) {
            state.item.forEach(item=>{
                if(item.id === id){
                    state.qwe -= item.sum
                }
            })
            state.item = state.item.filter(item => item.id !== id)
            setItem('ysy', state.item)
            setItem('qwe', state.qwe)
        },
        xiadan(state,i){
            state.xiadan1 = i
            
        }
    },
}